﻿
#region Copyright & License
// --------------------------
// Copyright 2008 Daniel Gray
// --------------------------
// This file is part of Kale.
//
// Kale is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Kale is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Kale.  If not, see <http://www.gnu.org/licenses/>.
#endregion
#region File details
// --------------------------------------------------------
// Filename          : SimpleNodeCache.cs
// Author            : Daniel Gray
// Created           : 15/04/2008
// --------------------------------------------------------
#endregion
            
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kale.Core
{
   /// <summary>
   /// Provides simple node caching services.
   /// </summary>
   /// <remarks>
   /// 
   /// </remarks>
   public class SimpleNodeCache : BaseNodeCache
   {

      private void RemoveStaleNode(NodeId id)
      {
         if (_cache.ContainsKey(id) && _cache[id].IsStale)
         {
            _cache.Remove(id);
         }
      }

      private Dictionary<NodeId, IImageNode> _cache = new Dictionary<NodeId, IImageNode>();

      public override void AddNode(IImageNode node)
      {
         lock (_cache)
         {
            if (!_cache.ContainsKey(node.NodeId))
            {
               _cache.Add(node.NodeId, node);
            }
            else
            {
               _cache[node.NodeId] = node;
            }
         }
      }

      public override void RemoveNode(IImageNode node)
      {
         RemoveNode(node.NodeId);
      }

      public override void RemoveNode(NodeId id)
      {
         lock (_cache)
         {
            if (_cache.ContainsKey(id))
            {
               _cache[id].MakeStale(NodeStaleReason.Removed);
               _cache.Remove(id);
            }
         }
      }

      public override void Clear()
      {
         lock (_cache)
         {
            _cache.Clear();
         }
      }

      public override bool HasNode(NodeId id)
      {
         lock (_cache)
         {
            RemoveStaleNode(id);
            
            return _cache.ContainsKey(id);
         }
      }

      public override IImageNode GetNode(NodeId id)
      {
         lock (_cache)
         {
            RemoveStaleNode(id);

            if (_cache.ContainsKey(id))
            {
               return _cache[id];
            }
            else
            {
               return null;
            }
         }
      }

      public override int Count
      {
         get { return _cache.Keys.Count; }
      }
   }
}
